package top.whysu.gps.controller.navigation;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.whysu.gps.dto.generate.RequestDTO;
import top.whysu.gps.dto.generate.ResponseDTO;
import top.whysu.gps.dto.navigation.*;
import top.whysu.gps.dto.page.PagenationDataDTO;
import top.whysu.gps.po.navigation.NavigationPO;
import top.whysu.gps.po.navigation.NavigationRecordPO;
import top.whysu.gps.service.navigation.NavigationRecordService;
import top.whysu.gps.service.navigation.NavigationService;
import top.whysu.gps.utils.ResponseUtil;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 航行记录
 */
@Controller
@RequestMapping("/navigationRecord")
public class NavigationRecordController {

    @Resource
    private NavigationRecordService navigationRecordService;
    @Resource
    private NavigationService navigationService;

    /**
     * 航行记录页面
     */
    @RequestMapping(value = "/navigationRecord")
    public String navigationRecord() {
        return "navigation/navigationRecord";
    }

    /**
     * 界面：航行管理--实时操作--设定航线
     */
    @RequestMapping(value = "/showMapFrame")
    public String showMapFrame(String navigationName, Map<String, Object> map) {
        map.put("navigationName", navigationName);
        return "navigation/frame/showMapFrame";
    }

    /**
     * 获取当前航线已发送的记录表
     *
     * @param queryDTO 当前航程id
     */
    @RequestMapping("/getCurrentRecordList")
    @ResponseBody
    public ResponseDTO<List<NavigationRecordPO>> getCurrentRecordList(@RequestBody @Validated CurrentRouteRecordQueryDTO queryDTO) {
        return ResponseUtil.getSuccessResponse(navigationRecordService.getCurrentRecordList(queryDTO.getCurrentNavigationId()));
    }

    /**
     * 获取当前航线已发送的记录表，限制条数
     *
     * @param queryDTO 当前航程id
     */
    @RequestMapping("/getLimitRecordList")
    @ResponseBody
    public ResponseDTO<List<NavigationRecordPO>> getLimitRecordList(@RequestBody @Validated CurrentRouteRecordQueryDTO queryDTO) {
        return ResponseUtil.getSuccessResponse(navigationRecordService.getLimitRecordList(queryDTO.getCurrentNavigationId()));
    }

    /**
     * 新增航行发送记录
     */
    @PostMapping("/addNavigationRecord")
    @ResponseBody
    public ResponseDTO addNavigationRecord(@RequestBody @Validated RequestDTO<NavigationRecordAddDTO> requestDTO) {
        navigationRecordService.addNavigationRecord(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 航程记录-二维表格
     */
    @RequestMapping("/getNavigationPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<NavigationPO>> getNavigationPage(@RequestBody NavigationPageQueryDTO queryDTO) {
        PagenationDataDTO<NavigationPO> page = navigationService.getNavigationPage(queryDTO);
        return ResponseUtil.getSuccessResponse(page);
    }

    /**
     * 航行记录-二维表格
     */
    @RequestMapping("/getNavigationRecordPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<NavigationRecordPO>> getNavigationRecordPage(@RequestBody NavigationRecordPageQueryDTO queryDTO) {
        PagenationDataDTO<NavigationRecordPO> page = navigationService.getNavigationRecordPage(queryDTO);
        return ResponseUtil.getSuccessResponse(page);
    }

    /**
     * 航行记录--坐标地图点位
     */
    @RequestMapping("/getNavigationPointMap")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<NavigationPointMapDTO>> getNavigationPointMap(@RequestBody @Valid NavigationRecordPageQueryDTO queryDTO) {
        PagenationDataDTO<NavigationPointMapDTO> page = navigationService.getNavigationPointMap(queryDTO);
        return ResponseUtil.getSuccessResponse(page);
    }

    /**
     * 获取最新一条为xx模式的发送记录
     */
    @PostMapping("/getLastSendData")
    @ResponseBody
    public ResponseDTO<NavigationRecordPO> getLastSendData(@RequestBody @Valid RequestDTO<LastSendDataQueryDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(navigationRecordService.selectLastSendData(requestDTO.getBody()));
    }

    /**
     * 批量删除航程记录（同时删除航行记录）
     */
    @PostMapping("/batchDeleteNavigationRecord")
    @ResponseBody
    public ResponseDTO batchDeleteNavigationRecord(@RequestBody @Valid RequestDTO<BatchDeleteNavigationDTO> requestDTO) {
        navigationService.batchDeleteNavigationRecord(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse();
    }
}
